package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp02list.ResultList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ResultListScwImpl implements ResultList {
    private static final Logger log = LoggerFactory.getLogger(ResultListScwImpl.class);


    private Result[] results;
    private int index;

    public ResultListScwImpl(int capacity) {
        results = new Result[capacity];
        index = 0;
    }


    public void add(Result result) {
        if (index + 1 == results.length) {
            Result[] newresult = new Result[results.length * 2];
            for (int i = 0; i < results.length; i++) {
                newresult[i] = results[i];
                results = newresult;
            }
        }
        results[index] = result;
        index++;
    }


    public Result get(int index) {
        if (index > size() - 1) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
        }
        return results[index];
    }


    public int size() {
        return index;
    }


    public Result findByGender(String gender) {
        for (int i = 0; i < index; i++) {
            if (get(i).getKey().equals(gender)) {
                return get(i);
            }
        }
        return null;
    }


    public Result findByDepartment(String department) {
        for (int i = 0; i < index; i++) {
            if (get(i).getKey().equals(department)) {
                return get(i);
            }
        }
        return null;
    }

    @Override
    public Result findByJobTitle(String jobTitle) {
        for (int i = 0; i < index; i++) {
            if (get(i).getKey().equals(jobTitle)) {
                return get(i);
            }
        }
        return null;
    }


}

